package com.dexing.likou;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Stack;

public class KuoHao {

    public static void main(String[] args) {
        System.out.println(isValid("()"));
    }

    public static boolean isValid(String s) {
        int n = s.length();
        if (n % 2 == 1) {
            return false;
        }
        Deque<String> stack = new LinkedList<>();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put(")", "(");
        stringStringHashMap.put("]", "[");
        stringStringHashMap.put("}", "{");
        for (int i = 0; i < n; i++) {
            String ch = String.valueOf(s.charAt(i));
            if (stringStringHashMap.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != stringStringHashMap.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
}
